package com.china08.yxyapi.api.manage.edu.platform.lesson.catalog;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.cloud.YxyLessonsCloudCatalog;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudCatalogRepository;
import com.china08.yxyapi.repository.db.lesson.YxyLessonsCloudLessonRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class EduLessonCatalogListController extends YxyApiBaseController {

    @Autowired
    YxyLessonsCloudCatalogRepository catalogRepository;

    @Autowired
    YxyLessonsCloudLessonRepository lessonRepository;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @ApiOperation(value = "")
    @RequestMapping(value = "/api_yw/manage/edu/platform/lesson/catalog/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response) {

        List<YxyLessonsCloudCatalog> list = catalogRepository.findBySchoolIdNotOrderByOrderNumAscCreatedDateAsc("");

        List<Catalog> catalog1List = new ArrayList<>();
        // 获取一级目录
        for (YxyLessonsCloudCatalog catalog : list) {
            int level = catalog.getLevel();
            if (level == 1) {
                Catalog catalog1 = new Catalog();
                catalog1.id = catalog.getId();
                catalog1.name = catalog.getName();
                catalog1.level = catalog.getLevel();
                catalog1.orderNum = catalog.getOrderNum();
                catalog1.pId = "0";
                catalog1.lessonCount = lessonCount(catalog.getId());
                catalog1List.add(catalog1);
            }
        }

        // 获取子分类
        for (Catalog catalog1 : catalog1List) {
            String cId1 = catalog1.id;
            // 获取二级分类
            List<Catalog> catalog2List = new ArrayList<>();
            for (YxyLessonsCloudCatalog cloudCatalog2 : list) {
                String id2 = cloudCatalog2.getId();
                String pid2 = cloudCatalog2.getPid();
                if (cId1.equals(pid2)) {
                    Catalog catalog2 = new Catalog();
                    catalog2.id = id2;
                    catalog2.name = cloudCatalog2.getName();
                    catalog2.level = cloudCatalog2.getLevel();
                    catalog2.orderNum = cloudCatalog2.getOrderNum();
                    catalog2.pId = pid2;
                    catalog2.lessonCount = lessonCount(id2);
                    catalog2List.add(catalog2);

                    // 获取三级栏目
                    List<Catalog> catalog3List = new ArrayList<>();
                    for (YxyLessonsCloudCatalog cloudCatalog3 : list) {
                        String id3 = cloudCatalog3.getId();
                        String pid3 = cloudCatalog3.getPid();
                        if (id2.equals(pid3)) {
                            Catalog catalog3 = new Catalog();
                            catalog3.id = id3;
                            catalog3.name = cloudCatalog3.getName();
                            catalog3.level = cloudCatalog3.getLevel();
                            catalog3.orderNum = cloudCatalog3.getOrderNum();
                            catalog3.pId = pid3;
                            catalog3.lessonCount = lessonCount(id3);
                            catalog3List.add(catalog3);

                            // 获取四级分类
                            List<Catalog> catalog4List = new ArrayList<>();
                            for (YxyLessonsCloudCatalog cloudCatalog4 : list) {
                                String id4 = cloudCatalog4.getId();
                                String pid4 = cloudCatalog4.getPid();
                                if (id3.equals(pid4)) {
                                    Catalog catalog4 = new Catalog();
                                    catalog4.id = id4;
                                    catalog4.name = cloudCatalog4.getName();
                                    catalog4.level = cloudCatalog4.getLevel();
                                    catalog4.orderNum = cloudCatalog4.getOrderNum();
                                    catalog4.pId = pid4;
                                    catalog4.lessonCount = lessonCount(id4);
                                    catalog4List.add(catalog4);

                                    // 获取五级分类
                                    List<Catalog> catalog5List = new ArrayList<>();
                                    for (YxyLessonsCloudCatalog cloudCatalog5 : list) {
                                        String id5 = cloudCatalog5.getId();
                                        String pid5 = cloudCatalog5.getPid();
                                        if (id4.equals(pid5)) {
                                            Catalog catalog5 = new Catalog();
                                            catalog5.id = id5;
                                            catalog5.name = cloudCatalog5.getName();
                                            catalog5.level = cloudCatalog5.getLevel();
                                            catalog5.orderNum = cloudCatalog5.getOrderNum();
                                            catalog5.pId = pid5;
                                            catalog5.lessonCount = lessonCount(id5);
                                            catalog5List.add(catalog5);
                                        }
                                    }
                                    catalog4.childCatalog = catalog5List;
                                }
                            }
                            catalog3.childCatalog = catalog4List;
                        }
                    }
                    catalog2.childCatalog = catalog3List;
                }
            }
            catalog1.childCatalog = catalog2List;
        }
        RespModel respModel = new RespModel();
        respModel.list = catalog1List;
        return respModel;
    }

    public Integer lessonCount(String catalogId) {
        StringBuffer sb = new StringBuffer();
        sb.append("   select                                            ");
        sb.append("     count(id)                                       ");
        sb.append("   from yxy_lessons.yxy_lessons_cloud_lesson         ");
        sb.append("   where (   status = 1                              ");
        sb.append("          or status = 2)                             ");
        sb.append("          and ( catalog_id1 = ?                      ");
        sb.append("             or catalog_id2 = ?                      ");
        sb.append("             or catalog_id3 = ?                      ");
        sb.append("             or catalog_id4 = ?                      ");
        sb.append("             or catalog_id5 = ?)                     ");
        return jdbcTemplate.queryForObject(sb.toString(), new Object[] { catalogId, catalogId, catalogId, catalogId, catalogId }, Integer.class);
    }

    public static class Catalog {
        public String id;
        public String name;
        public Integer lessonCount;
        public Integer orderNum;
        public Integer level;
        public String pId;
        public Integer childCatalogCount;
        public List<Catalog> childCatalog;
    }

    public static class RespModel extends YxyApiRespModel {
        public List<Catalog> list;
    }
}
